"""
    Author:
    Date: 2019-03-12

    微信管理后台-用户排行
"""

from django.shortcuts import render

from entities.consts import *
from entities import user_mgr
from utils.log import Log

from .. import helpers
from .. helpers import gma_login_required


@gma_login_required
def index(request):
    """ 首页 """
    context = helpers.build_context(request)
    context['total_caster_count'] = user_mgr.get_total_broadcaster_count(request=request)
    context['recommend_caster_count'] = user_mgr.get_recommend_user_count(request=request)
    context['total_watcher_count'] = user_mgr.get_total_watcher_count(request=request)
    context['total_vip_count'] = user_mgr.get_total_vip_count(request=request)
    context['total_banned_caster_count'] = user_mgr.get_banned_user_count(request=request)
    context['total_allow_free_chat_caster_count'] = user_mgr.get_allow_free_chat_caster_count(request=request)
    return render(request, 'gma/ranking/ranking_home.html', context)


@gma_login_required
def casters_ranking(request, ranking_type):
    """ 主播排行榜 """
    ranking_type = int(ranking_type)

    count = 100
    casters = []
    if ranking_type == RANKING_DROP_CALL:
        casters = user_mgr.get_user_list_by_drop_count(count, request=request)
    else:
        Log.error(f'Invalid caster ranking type: {ranking_type}')
        return helpers.show_page_tips(request, f'无效的排行类型: {ranking_type}')

    for caster in casters:
        caster['is_ios_device'] = 'iphone' in caster['last_login_phone'].lower()

    context = helpers.build_context(request)
    context['casters'] = casters
    context['total_count'] = count
    return render(request, 'gma/ranking/caster_list.html', context)


@gma_login_required
def drop_call_log(request, uid):
    """ 漏接电话记录 """
    context = helpers.build_context(request)
    context['user'] = user_mgr.get_user_detail(uid)
    context['logs'] = user_mgr.get_broadcaster_dropped_call_records(uid, 200)
    context['total_dropped_call_count'] = user_mgr.get_broadcaster_dropped_call_count(uid)
    return render(request, 'gma/ranking/caster_call_detail.html', context)


if __name__ == '__main__':
    print('ok')
